package com.cdu.practicaltraining.sys.service.impl;

import com.cdu.practicaltraining.pojo.Dict;
import com.cdu.practicaltraining.pojo.DictDetail;
import com.cdu.practicaltraining.sys.mapper.DictDetailMapper;
import com.cdu.practicaltraining.sys.mapper.DictMapper;
import com.cdu.practicaltraining.sys.service.DictService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl implements DictService {
    // 注入Mapper
    @Resource
    private DictMapper dictMapper;
    @Resource
    private DictDetailMapper dictDetailMapper;

    @Override
    public int addDict(Dict dict) throws Exception {
        return dictMapper.addDict(dict);
    }

    @Override
    public Dict findDict(String dname) throws Exception {
        return dictMapper.findDict(dname);
    }

    // 添加字典的业务层逻辑
    @Override
    public int insertDict(Dict dict) throws Exception {
        // 添加的时候先判断是否存在要添加的字典
        Dict db_dict = findDict(dict.getDname());
        // if (null == db_dict) {
        //     // 如果是空的，表示数据库中不存在要添加的字典，可以添加
        //     return addDict(dict);
        // }
        // return -1;

        return null == db_dict ? addDict(dict) : -1;
    }

    @Override
    public List<Dict> dicts() throws Exception {
        return dictMapper.dicts();
    }

    @Override
    public Dict findById(int id) throws Exception {
        return dictMapper.findById(id);
    }
    @Override
    public int updateDict(Dict dict) throws Exception {
        return dictMapper.updateDict(dict);
    }

    @Override
    public int deleteDict(long id) throws Exception {
        return dictMapper.deleteDict(id);
    }

    @Override
    public int insertDictDetail(DictDetail dictDetail) throws Exception {
        DictDetail detail = findDictDetail(dictDetail.getDtype(), dictDetail.getDtag());
        return null == detail ? addDictDetail(dictDetail) : 0;
    }

    @Override
    public int addDictDetail(DictDetail dictDetail) throws Exception {
        return dictDetailMapper.addDictDetail(dictDetail);
    }

    @Override
    public DictDetail findDictDetail(String dtype, String dtag) throws Exception {
        return dictDetailMapper.findDictDetail(dtype, dtag);
    }

    @Override
    public List<DictDetail> findDictDetailByType(String dtype) throws Exception {
        return dictDetailMapper.findDictDetailByType(dtype);
    }
}
